package com.vue;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.controleur.listener.impl.MenuPanRestaurantListener;
import com.controleur.listener.impl.RestaurantControler;
import com.controleur.listener.impl.RestaurantModifierListener;
import com.modele.entity.Restaurant;
import com.modele.entity.TypeRestaurant;

public class RestaurantModifierVue  extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel titreLabel,nomLabel,descLabel,typeLabel,villeLabel,numeroLabel,rueLabel,cpLabel,siteLabel,telLabel;
	private JTextField nom,desc,rue,numero,ville,cp,site,tel;
	private JButton sauvegarderButton, annulerButton;
	private Restaurant restaurant;
	JComboBox type;
	Fenetre fenetre;
	
    public RestaurantModifierVue(Fenetre f) {
    	this.fenetre = f;
		this.initialize();
		this.build();
    }
    public JTextField getSite() {
		return site;
	}
	public void setSite(JTextField site) {
		this.site = site;
	}
	public JTextField getTel() {
		return tel;
	}
	public void setTel(JTextField tel) {
		this.tel = tel;
	}
	public RestaurantModifierVue(Fenetre f,String nom) {
    	RestaurantControler controler = new RestaurantControler();
    	this.fenetre = f;
    	this.restaurant=controler.findByName(nom);
    	this.initialize();
		this.build();
    }
	public void initialize() {
	
        /* 1- Initialisation du container. */
        this.setLayout(new GridBagLayout());

        /* 2- Création et initialisation d'une série de composants. */

        titreLabel = new JLabel("Modifier un restaurant :");
        nomLabel = new JLabel("Nom du restaurant :");
        typeLabel = new JLabel("Type :");
        descLabel = new JLabel("Description :");
        villeLabel = new JLabel("Ville :");
        numeroLabel = new JLabel("numéro :");
        rueLabel = new JLabel("rue :");
        cpLabel = new JLabel("Code postale :");
        siteLabel = new JLabel("site :");
        telLabel = new JLabel("tel :");
        
        nom = new JTextField(10);
        nom.setText(restaurant.getNom());
		desc = new JTextField(10);
		desc.setText(restaurant.getDescription());
        site = new JTextField(10);
        site.setText(restaurant.getSite());
		tel = new JTextField(10);
		tel.setText(restaurant.getTel());
		ville = new JTextField(10);
		ville.setText(restaurant.getVille());
		numero = new JTextField(10);
		numero.setText(restaurant.getNum());
		rue = new JTextField(10);
		rue.setText(restaurant.getRue());
		cp = new JTextField(10);
		cp.setText(restaurant.getCp());
		type = new JComboBox();
		ArrayList<TypeRestaurant> listeType = new RestaurantControler().findAllType();
		for(int i=0;i<listeType.size();i++){
			type.addItem(listeType.get(i).getTypeResto().toString());
		}
		type.setSelectedItem(restaurant.getTypeRestaurant().getTypeResto());
		sauvegarderButton = new JButton("Sauvegarder");
        annulerButton = new JButton("Annuler");	
	}
	public void build(){
		 /*3- Ajout de ces composants en spécifiant les contraintes de type GridBagConstraints. */
        GridBagConstraints gbc = new GridBagConstraints();
        
        //titre de la page
        Font titreFont = new Font("Serif", Font.PLAIN, 26);
        titreLabel.setFont(titreFont);
        gbc.gridx = gbc.gridy = 0; 
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        gbc.anchor = GridBagConstraints.CENTER; 
        gbc.insets = new Insets(15, 0, 15, 0); 
        this.add(titreLabel, gbc);
        
        //nom label
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(nomLabel, gbc);

        //nom textField
        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(nom, gbc);
        
        //type label 
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(typeLabel, gbc);
        
        //type textfield
        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        type.setEditable(false);
        this.add(type, gbc);

        //tel label
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(telLabel, gbc);

        //tel textField
        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(tel, gbc);
        
        //site label
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(siteLabel, gbc);

        //site textField
        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(site, gbc);
        
        //description label
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(descLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(desc, gbc);

        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(villeLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(ville, gbc);

        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(numeroLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(numero, gbc);

        gbc.gridx = 0;
        gbc.gridy = 8;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(rueLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(rue, gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 9;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 10, 0, 0);
        this.add(cpLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 0, 10);
        this.add(cp, gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 18;
        gbc.gridheight = gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
        gbc.weightx = 1.;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(10, 0, 0, 5);
        annulerButton.addActionListener(new MenuPanRestaurantListener());
        this.add(annulerButton, gbc);
        
        
        gbc.gridx = 2;
        gbc.weightx = 0;
        gbc.insets = new Insets(10, 0, 0, 10);
        sauvegarderButton.addActionListener(new RestaurantModifierListener(fenetre));
        this.add(sauvegarderButton, gbc);

        this.setMinimumSize(new Dimension(500, 325));

	}
	
    public JTextField getNom() {
		return nom;
	}

	public void setNom(JTextField nom) {
		this.nom = nom;
	}

	public JTextField getDesc() {
		return desc;
	}

	public void setDesc(JTextField desc) {
		this.desc = desc;
	}

	public JTextField getRue() {
		return rue;
	}

	public void setRue(JTextField rue) {
		this.rue = rue;
	}

	public JTextField getNumero() {
		return numero;
	}

	public void setNumero(JTextField numero) {
		this.numero = numero;
	}

	public JTextField getVille() {
		return ville;
	}

	public void setVille(JTextField ville) {
		this.ville = ville;
	}

	public JTextField getCp() {
		return cp;
	}

	public void setCp(JTextField cp) {
		this.cp = cp;
	}

	public JComboBox getType() {;
		return type;
	}

	public void setType(JComboBox type) {
		this.type = type;
	}

public Restaurant getRestaurant() {
	return this.restaurant;
}

}
